﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ACPLogAnalyzer
{
    /// <summary>
    /// Interaction logic for Help.xaml
    /// </summary>
    public partial class Update : Window
    {
        #region Properties
        private bool m_readToInstall;
        public bool ReadToInstall
        {
            get { return m_readToInstall; }
            set { m_readToInstall = value; }
        }

        private bool m_downloading;
        public bool Downloading
        {
            get { return m_downloading; }
            set { m_downloading = value; }
        }

        private AppVersionChecker m_appVersionChecker;
        public AppVersionChecker AppVersionChecker
        {
            get { return m_appVersionChecker; }
            set { m_appVersionChecker = value; }
        }
        #endregion

        public Update(AppVersionChecker avc)
        {
            InitializeComponent();

            m_downloading = false;
            m_readToInstall = false;
            m_appVersionChecker = avc;
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            progressBarDownload.Visibility = System.Windows.Visibility.Hidden;

            textBoxRunning.Text = AppVersionChecker.RunningVersion.ToString();
            textBoxAvailable.Text = AppVersionChecker.AvailableVersion.ToString();

            if (AppVersionChecker.IsUpdateAvailable)
            {

                // Have we already downloaded the update?
                string path = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Temp\" +
                    System.IO.Path.GetFileName(Properties.Settings.Default.Update_Download_Url);

                if (System.IO.File.Exists(path))
                {
                    m_appVersionChecker_DownloadComplete(this, System.EventArgs.Empty);
                    buttonDownloadUpdate_Click(this, null);
                }
                else
                    labelTitle.Content = "An update is available";
            }
            else
            {
                buttonClose.Content = "Close";
                buttonDownloadUpdate.Visibility = System.Windows.Visibility.Hidden;
                labelTitle.Content = "You are running the latest version";
            }
        }

        private void buttonDownloadUpdate_Click(object sender, RoutedEventArgs e)
        {
            if (!m_downloading)
            {
                m_downloading = true;
                buttonClose.Visibility = System.Windows.Visibility.Hidden;
                buttonDownloadUpdate.Content = "Stop Download";
                labelTitle.Visibility = System.Windows.Visibility.Visible;
                progressBarDownload.Visibility = System.Windows.Visibility.Visible;

                m_appVersionChecker.DownloadProgress += 
                    new EventHandler<System.Net.DownloadProgressChangedEventArgs>(m_appVersionChecker_DownloadProgress);

                m_appVersionChecker.DownloadComplete += 
                    new EventHandler(m_appVersionChecker_DownloadComplete);

                m_appVersionChecker.StartUpdateDownload();
            }
            else if (m_readToInstall)
            {
                // Quit and do the install
                string path = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Temp\" +
                    System.IO.Path.GetFileName(Properties.Settings.Default.Update_Download_Url);

                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(path));
                Application.Current.Shutdown();
            }
            else
            {
                this.Close();
            }
        }

        void m_appVersionChecker_DownloadComplete(object sender, EventArgs e)
        {
            m_readToInstall = true;
            buttonDownloadUpdate.Content = "Quit and Install Update";
            buttonClose.Content = "Don't Install Update";
            buttonClose.Visibility = System.Windows.Visibility.Visible;
            labelTitle.Content = "Ready to install update (downloaded)";
        }

        void m_appVersionChecker_DownloadProgress(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            labelTitle.Content = "Downloading (" + e.ProgressPercentage.ToString() + "%)...";
            progressBarDownload.Value = e.ProgressPercentage;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Release notes...
            webBrowserRelNotes.Source = new Uri(Properties.Settings.Default.Release_Notes_Url);
        }

        private void webBrowserRelNotes_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            webBrowserRelNotes.Refresh(true);
        }
    }
}
